package user;

import java.io.Serializable;
import java.util.EnumMap;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

/**
 *
 * @author Thorsten Ludwig
 */
@Entity
public class Rights implements Serializable  {

    @Cascade(CascadeType.ALL)
    private EnumMap<Role,Boolean> hasViewRights;
    @Cascade(CascadeType.ALL)
    private EnumMap<Role,Boolean> hasEditRights;
    @Id
    @GeneratedValue    
    private Long id;
    
    public Rights() {
        this.hasEditRights = new EnumMap<Role, Boolean>(Role.class);
        this.hasViewRights = new EnumMap<Role, Boolean>(Role.class);
        this.initFields();
    }
    
    /**
     * 
     * @param role 
     */
    public void giveViewRights(Role role) {
        this.hasViewRights.put(role, true);
    }
    
    /**
     * 
     * @param role 
     */
    public void removeViewRights(Role role) {
        this.hasViewRights.put(role, false); 
    }
    
    /**
     * 
     * @param role 
     */
    public void giveEditRights(Role role ) {
        this.hasEditRights.put(role,true); 
    }
    
    /**
     * 
     * @param role 
     */
    public void removeEditRights(Role role) {
        this.hasEditRights.put(role,false);
    }
    
    /**
     * 
     * @param role
     * @return 
     */
    public boolean hasEditRight(Role role) {
        return (this.hasEditRights.get(role));
    }
    
    /**
     * 
     * @param role
     * @return 
     */
    public boolean hasViewRight(Role role) {
        return this.hasViewRights.get(role);
    }

    private void initFields() {
        this.removeEditRights(Role.ADMIN);
        this.removeEditRights(Role.COADMIN);
        this.removeEditRights(Role.TUTOR);
        this.removeEditRights(Role.STUDENT);
        this.removeEditRights(Role.GUEST);
        
        this.removeViewRights(Role.ADMIN);
        this.removeViewRights(Role.COADMIN);
        this.removeViewRights(Role.TUTOR);
        this.removeViewRights(Role.STUDENT);
        this.removeViewRights(Role.GUEST);
        
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public EnumMap<Role, Boolean> getHasEditRights() {
        return hasEditRights;
    }

    public void setHasEditRights(EnumMap<Role, Boolean> hasEditRights) {
        this.hasEditRights = hasEditRights;
    }

    public EnumMap<Role, Boolean> getHasViewRights() {
        return hasViewRights;
    }

    public void setHasViewRights(EnumMap<Role, Boolean> hasViewRights) {
        this.hasViewRights = hasViewRights;
    }
}
